package com.taptech.doufu.ui.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.adapter.NovelSortAdapter;
import com.taptech.doufu.bean.NovelSortBean;
import com.taptech.doufu.bean.NovelSortUpBean;
import com.taptech.doufu.util.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${LiYi} on 2017/1/4.
 */

public class IntelligentSortPopWindow extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private List<NovelSortUpBean>  novelSortUpBeanList = new ArrayList<>();
    private View outLayout;
    private LinearLayout contentLayout;
    private IntelligentSortSelSort intelligentSortSelSortListener;
    private List<NovelSortAdapter> adapterList = new ArrayList<>();

    public IntelligentSortPopWindow(Context mContext ,List<NovelSortUpBean> novelSortUpBeanList){
        super(mContext);
        this.mContext = mContext;
        if (novelSortUpBeanList != null) {
            this.novelSortUpBeanList.addAll(novelSortUpBeanList);
        }
        initView();
        initDate();
    }

    public void setIntelligentSortSelSortListener(IntelligentSortSelSort intelligentSortSelSortListener) {
        this.intelligentSortSelSortListener = intelligentSortSelSortListener;
    }

    public void setNovelSortUpBeanList(List<NovelSortUpBean> novelSortUpBeanList) {
        this.novelSortUpBeanList.clear();
    }

    private void initView(){
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final  View contentView = inflater.inflate(R.layout.sort_intelligent_pop_layout, null);
        contentLayout = (LinearLayout) contentView.findViewById(R.id.sort_intellingent_content_layout);
        outLayout =  contentView.findViewById(R.id.sort_intellingent_out_layout);
        contentView.findViewById(R.id.sort_reset_btn).setOnClickListener(this);
        contentView.findViewById(R.id.sort_sure_btn).setOnClickListener(this);
        //设置SelectPicPopupWindow的View
        this.setContentView(contentView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.Animation_Top_Bottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        outLayout.setOnClickListener(this);
    }

    private void initDate(){
        adapterList.clear();
        for( int i = 0 ; i< novelSortUpBeanList.size(); i++){

            final  NovelSortUpBean novelSortUpBean = novelSortUpBeanList.get(i);
            View view = LayoutInflater.from(mContext).inflate(R.layout.sort_intelligent_item,null);
            TextView titleTv = (TextView) view.findViewById(R.id.sort_intellingent_item_title);
            View  lineView = view.findViewById(R.id.sort_intelligent_item_line);
            if(i == novelSortUpBeanList.size()-1){
                LinearLayout.LayoutParams lineLp = new LinearLayout.LayoutParams(-1, ScreenUtil.dip2px(mContext,0.5f));
                lineLp.setMargins(0,ScreenUtil.dip2px(mContext,12),0,0);
                lineView.setLayoutParams(lineLp);
            }

            NoScrollGridView noScrollGridView = (NoScrollGridView) view.findViewById(R.id.sort_intellingent_item_gdView);
            noScrollGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    NovelSortAdapter sortAdapter = (NovelSortAdapter)parent.getAdapter();
                    NovelSortBean novelSortBean = sortAdapter.getItem(position);

                    int index = novelSortUpBeanList.indexOf(novelSortUpBean);
                    if(index > -1 && index < novelSortUpBeanList.size()){
                        novelSortUpBean.setSelNovelSortBean(novelSortBean);
                        novelSortUpBeanList.set(index , novelSortUpBean);
                    }

                    sortAdapter.setSelNovelSortBean(novelSortBean);
                    sortAdapter.notifyDataSetChanged();
                }
            });

            List<NovelSortBean> novelSortBeanList = novelSortUpBean.getList();
            NovelSortBean selNovelSortBean = novelSortUpBean .getSelNovelSortBean();
            if(selNovelSortBean == null && novelSortBeanList != null && novelSortBeanList.size() > 0){
                selNovelSortBean = novelSortBeanList.get(0);
                novelSortUpBean.setSelNovelSortBean(selNovelSortBean);
            }

            NovelSortAdapter sortAdapter = new NovelSortAdapter(mContext,novelSortUpBean.getList());
            sortAdapter.setSelNovelSortBean(selNovelSortBean);
            noScrollGridView.setAdapter(sortAdapter);
            titleTv.setText(novelSortUpBean.getTitle());
            adapterList.add(sortAdapter);
            contentLayout.addView(view);
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sort_reset_btn:
                resetData();
                break;
            case R.id.sort_sure_btn:
                if(intelligentSortSelSortListener != null){
                    intelligentSortSelSortListener.selSortListener(novelSortUpBeanList);
                }
                dismiss();
                break;
            case R.id.sort_intellingent_out_layout:
                dismiss();
                break;
        }

    }

    private void resetData(){
        for(NovelSortUpBean  novelSortUpBean : novelSortUpBeanList){
            List<NovelSortBean> novelSortBeanList = novelSortUpBean.getList();
            if(novelSortBeanList != null && novelSortBeanList.size() > 0){
                novelSortUpBean.setSelNovelSortBean(novelSortBeanList.get(0));
            }
        }
        for(int i = 0 ;i < adapterList.size() ; i++){
            NovelSortAdapter adapter = adapterList.get(i);

            if(novelSortUpBeanList != null && i < novelSortUpBeanList.size()){
                NovelSortBean selSortBean = novelSortUpBeanList.get(i).getSelNovelSortBean();
                if(adapter != null){
                    adapter.setSelNovelSortBean(selSortBean);
                    adapter.notifyDataSetInvalidated();
                }
            }
        }
    }

    public interface IntelligentSortSelSort{
        public void selSortListener(List<NovelSortUpBean> novelSortUpBeanList);
    }
}
